<?php
/************************************************
/   Project:
/   Created by: ScriptPlanet.eu
/   Support: See component admin
/   Site: http://www.scriptplanet.eu
/   Version:
************************************************/
// Joomla Security
defined('_JEXEC') or die;
JHTML::_('behavior.mootools');
jimport('joomla.application.component.helper');
if(JComponentHelper::isEnabled('com_joosam', true)):

	$partitle = $params->get('artTit');
	$parData = $params->get('artTxt');
	$parBg0 = $params->get('bgFirRow');
	$parBgHv0 = $params->get('bgFirRowHv');
	$parBg1 = $params->get('bgSecRow');
	$parBgHv1 = $params->get('bgSecRowHv');

	echo '<style type="text/css">';
	echo '
	.jsam_mod_history .jsam_row .jsam_td_artist, .jsam_mod_history .jsam_row .jsam_td_title{ width:50px; margin:0; padding:0 4px 0 0; text-align:left; color:#'.$partitle.'; }
	.jsam_mod_history .jsam_row .jsam_td_artist_data, .jsam_mod_history .jsam_row .jsam_td_title_data{ margin:0; padding:0; text-align:left; color:#'.$parData.'; }
	.jsam_mod_history .jsam_table_bg0, .jsam_mod_history .jsam_table_bg1{ width:100%; margin:0 0 5px 0; padding:0; }';
	if($parBg0 != 'FFFFFF' AND $parBg0 != ''){ echo '.jsam_mod_history .jsam_table_bg0{ background:#'.$parBg0.'; margin:0; }'; }
	if($parBgHv0 != 'FFFFFF' AND $parBgHv0 != ''){ echo '.jsam_mod_history .jsam_table_bg0:hover{ background:#'.$parBgHv0.'; }'; }
	if($parBg1 != 'FFFFFF' AND $parBg1 != ''){ echo '.jsam_mod_history .jsam_table_bg1{ background:#'.$parBg1.'; margin:0; }'; }
	if($parBgHv1 != 'FFFFFF' AND $parBgHv1 != ''){ echo '.jsam_mod_history .jsam_table_bg1:hover{ background:#'.$parBgHv1.'; }'; }
	echo '</style>';
	?>

	<div id="jsamHistory" class="jsam_mod_history"></div>

	<script>
	// Object
	function jsamHistObject(){
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
		}
		return req;
	}
	// var that load the objects
	var jsamHis = jsamHistObject();
	// ajax load php for history
	function jsamHistSend(url){
		jsamHis.open('post', url ,true);
		jsamHis.onreadystatechange = jsamHistHandleResponse;
		jsamHis.send(null);
	}
	// on ready jsam history
	function jsamHistHandleResponse(){
		if(jsamHis.readyState == 4 && jsamHis.status == 200){
			var target = document.getElementById("jsamHistory");
			var response = eval("(" + jsamHis.responseText + ")");
			if(response){
				// Create song data
				var sort = response.type;
				var artist = response.artist;
				var title = response.title;
				var scount = response.count;
				var song1_date = response.played1;
				var song1_artist = response.artist1;
				var song1_title = response.title1;
				var song2_date = response.played2;
				var song2_artist = response.artist2;
				var song2_title = response.title2;
				var song3_date = response.played3;
				var song3_artist = response.artist3;
				var song3_title = response.title3;
				var song4_date = response.played4;
				var song4_artist = response.artist4;
				var song4_title = response.title4;
				var song5_date = response.played5;
				var song5_artist = response.artist5;
				var song5_title = response.title5;
				scount = parseFloat(scount);

				if(sort == 'passed'){
					var output = '';

					if(scount >= 1){
					
						output = output+'<table border="0" class="jsam_table_bg0">'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_date">'+song1_date+'</th>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+artist+'</td>'+
										'<td class="jsam_td_artist_data">'+song1_artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+title+'</td>'+
										'<td class="jsam_td_title_data">'+song1_title+'</td>'+
									'</tr>'+
								'</table>';
					}
					if(scount >= 2){
					
						output = output+'<table border="0" class="jsam_table_bg1">'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_date">'+song2_date+'</th>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+artist+'</td>'+
										'<td class="jsam_td_artist_data">'+song2_artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+title+'</td>'+
										'<td class="jsam_td_title_data">'+song2_title+'</td>'+
									'</tr>'+
								'</table>';
					}
					if(scount >= 3){
					
						output = output+'<table border="0" class="jsam_table_bg0">'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_date">'+song3_date+'</th>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+artist+'</td>'+
										'<td class="jsam_td_artist_data">'+song3_artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+title+'</td>'+
										'<td class="jsam_td_title_data">'+song3_title+'</td>'+
									'</tr>'+
								'</table>';
					}
					if(scount >= 4){
					
						output = output+'<table border="0" class="jsam_table_bg1">'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_date">'+song4_date+'</th>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+artist+'</td>'+
										'<td class="jsam_td_artist_data">'+song4_artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+title+'</td>'+
										'<td class="jsam_td_title_data">'+song4_title+'</td>'+
									'</tr>'+
								'</table>';
					}
					if(scount >= 5){
					
						output = output+'<table border="0" class="jsam_table_bg0">'+
									'<tr class="jsam_row">'+
										'<th colspan="2" class="jsam_th_date">'+song5_date+'</th>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_artist">'+artist+'</td>'+
										'<td class="jsam_td_artist_data">'+song5_artist+'</td>'+
									'</tr>'+
									'<tr class="jsam_row">'+
										'<td class="jsam_td_title">'+title+'</td>'+
										'<td class="jsam_td_title_data">'+song5_title+'</td>'+
									'</tr>'+
								'</table>';
					}

					target.innerHTML = output;
				}
			}
		}
	}
	// set auto refresh for history - 10sec
	function jsamHistRefresh(){
		jsamHistSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=history&format=raw'); ?>");
		setTimeout('jsamHistRefresh()', 10000);
	}
	// window onload for history
	window.onload = jsamHistRefresh();
	</script>
<?php else: ?>
	<div id="jsamHistory">
		<div style="background:white; color:red; font-weight:bold; border-radius:10px; margin:1px; text-align:center; padding:0;">
			Component joosam is not installed.
		</div>
	</div><?php
endif;
?>